<?php

namespace model;

use common\Model;

class images extends Model
{
	const TABLE = "images";
	private $DB;
    function __construct()
    {
        $this->DB = parent::db();
    }

	public function findData()
	{
		return $this->DB->table(self::TABLE)->where(['id'=>$_GET['id']])->find();
	}

	//获取数据并分页
	public function jsonPageSelect()
	{
		$p = intval($_GET['page'] ?? 0) ?: 1;
		$num = intval($_GET['limit'] ?? 0) ?: 15;

		$page = ['num' => $num, 'p' => $p, 'return' => true];
		return [
			'data' => $this->DB->table(self::TABLE)->Page($page)->order('id DESC')->select(),
			'page' => $this->DB->getPage()
		];
	}

	//添加数据
	public function insertData()
	{
		if (empty($_POST['title']))
			return ['status' => 0, 'msg' => '请填写必要数据'];
		$data = [
			'title' => VarFilter($_POST['title']),
			'img' => VarFilter($_POST['img']),
			'auther' => VarFilter($_POST['auther']),
			'content' => VarFilter($_POST['content']),
			'time' => date('Y-m-d H:i:s', time()),
			'status' => $_POST['status'],
		];
		$re_key = $this->DB->table(self::TABLE)->insert($data);
		if ($re_key) {
			SqlLog("添加图集 ID：" . $re_key);
			return ['status' => 1, 'msg' => '添加成功'];
		} else {
			return ['status' => 0, 'msg' => '数据错误！添加失败'];
		}
	}
	//更新数据
	public function updateData()
	{
		if (empty($_POST['title']))
			return  ['status' => 0, 'msg' => '请填写必要数据'];
		$data = [
			'title' => VarFilter($_POST['title']),
			'img' => VarFilter($_POST['img']),
			'auther' => VarFilter($_POST['auther']),
			'content' => VarFilter($_POST['content']),
			'status' => $_POST['status'],
		];
		$re_key = $this->DB->table(self::TABLE)->Where(['id' => $_POST['id']])->Update($data);
		if ($re_key > 0) {
			SqlLog("修改图集 ID：" . $_POST['id']);
			return ['status' => 1, 'msg' => '修改成功'];
		} else {
			return ['status' => 0, 'msg' => '数据没有变化'];
		}
	}

	//判断删除一条数据
	public function deleteOneData()
	{
		$find = $this->DB->table('images_image')->where(['aid' => $_POST['id']])->find();
		if ($find) {
			return false;
		} else {
			SqlLog("删除图集 ID：" . $_POST['id']); //写入日志
			return $this->DB->table(self::TABLE)->where(['id' => $_POST['id']])->Delete();
		}
	}

	//设置状态
	public function switchStatus()
	{
		$data = [
			'status' => $_POST['status']
		];
		SqlLog("设置图集状态 ID：" . $_POST['id']);
		return $this->DB->table(self::TABLE)->where(['id' => $_POST['id']])->Update($data);
	}

	
}
